package com.utils
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	
	/**
	 * 文本添加颜色渐变效果 
	 * @author Mr.Lu
	 */	
	public class TextGradualColor extends Sprite{
		private var _matrix:Matrix;
		private var _shape:Shape;
		private var _targetText:TextField;
		
		/**
		 * 
		 * @param targetText
		 * @param rotation Math.PI/2
		 * @param colors
		 * @param alphas
		 * @param ratios
		 * 
		 */		
		public function TextGradualColor(targetText:TextField,rotation:Number=Math.PI/2,colors:Array=null,alphas:Array=null,ratios:Array = null,glowFilter:Array=null){
			if(colors && alphas && ratios && (colors.length != alphas.length || colors.length != ratios.length)){
				throw Error('颜色属性数组长度不一致!')
			}
			_targetText = setTextFormat(targetText,{cacheAsBitmap:true,mouseEnabled:false});
			_targetText.width = _targetText.textWidth +5;
			_targetText.height = _targetText.textHeight +5;
			_shape = new Shape();
			_shape.cacheAsBitmap = true;
			if(!colors) colors = [0xFF0000,0xFFFF00,0xFF0000];
			if(!alphas) alphas = [1,1,1];
			if(!ratios) ratios = [0,122,255];
			drawGradientShape(_shape,_targetText.width,_targetText.height,0,0,rotation,0,0,0,0,0,'linear',colors,alphas,ratios);
			this.addChild(_shape);
			this.addChild(_targetText);
			_shape.mask = _targetText;
			this.filters = glowFilter ? glowFilter :[];
			this.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStageHandler);
		}
		
		private function onRemoveFromStageHandler(e:Event):void
		{
			this.filters = [];
			this.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStageHandler);
			while(this.numChildren) removeChildAt(0);
			_shape = null;
			_targetText = null;
			_matrix = null;
		}
		
		private function setTextFormat(target:TextField,params:Object=null):TextField
		{
			for(var key:String in params){
				try{
					target[key] = params[key];
				}catch(e:Error){
					trace('Error:' + key);
				}
			}
			return target;
		}
		
		/**
		 * 绘制渐变色条
		 * @param target 目标
		 * @param width 宽度
		 * @param heigth 高度
		 * @param matrixOffsetX X方向位移
		 * @param matrixOffsetY Y方向位移
		 * @param matrixRotation 旋转角度 单位：弧度
		 * 
		 * @param lineThickness 边缘线粗度
		 * @param lineColor 颜色
		 * @param lineAlpha 透明度
		 * @param ellipseWidth 横向圆角大小
		 * @param ellipseHeight 纵向圆角大小
		 * 
		 * @param fillType 渐变类型 linear/radial
		 * @param colors 渐变色数组 [0xcc0000,0x330000,0xcc0000]
		 * @param alphas 透明度数组 [1,1,1]
		 * @param ratios 颜色分布比率 [0,122,255]
		 * @param spreadMethod 
		 * @param interpolationMethod
		 * @param focalPointRatio
		 */
		private function drawGradientShape(target:*,width:int,heigth:int,matrixOffsetX:int = 0,matrixOffsetY:int = 0,matrixRotation:Number = Math.PI / 2,
										   lineThickness:Number = 1,lineColor:uint = 0,lineAlpha:Number = 1,ellipseWidth:int = 20,ellipseHeight:int= 20,
										   fillType:String='linear',colors:Array = null,alphas:Array = null,ratios:Array = null,spreadMethod:String="pad", interpolationMethod:String="rgb", focalPointRatio:Number=0):void
		{
			if(!_matrix) _matrix = new Matrix();
			_matrix.createGradientBox(width,heigth,matrixRotation,matrixOffsetX,matrixOffsetY);
			target.graphics.clear();
			target.graphics.lineStyle(lineThickness,lineColor,lineAlpha);
			target.graphics.beginGradientFill(fillType,colors,alphas,ratios,_matrix,'pad','rgb',0);
			target.graphics.drawRoundRect(0,0,width,heigth,ellipseWidth,ellipseHeight);
			target.graphics.endFill();
		}
		
	}
}
